package com.xinggq.pay.service.controller;

import cn.hutool.core.lang.Validator;
import com.xinggq.common.CommonResult;
import com.xinggq.pay.service.entity.PayOrderEntity;
import com.xinggq.pay.service.enums.PayChannelEnum;
import com.xinggq.pay.service.pay.channel.yse.other.division.YseDivisionServiceImpl;
import com.xinggq.pay.service.pay.channel.yse.other.division.dto.DivisionParamDTO;
import com.xinggq.pay.service.pay.channel.yse.other.division.dto.DivisionRespDTO;
import com.xinggq.pay.service.service.PayOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description
 * @Author xinggq
 * @Date 2025/9/19
 */
@Tag(name = "银盛支付")
@RestController
@RequestMapping("/api/yse")
@Slf4j
@RequiredArgsConstructor
public class YseDivisionController {

  private final YseDivisionServiceImpl yseDivisionService;
  private final PayOrderService payOrderService;


  @Operation(summary = "再次分账接口，仅供运维使用")
  @GetMapping("/division/again")
  public CommonResult<DivisionRespDTO> divisionAgain(@Parameter(description = "支付渠道订单号")
      @RequestParam(value = "channelOutTradeNo") String channelOutTradeNo) {

    PayOrderEntity order =  payOrderService.findByChannelTradeNoAndChannelType(
        channelOutTradeNo, PayChannelEnum.YSE);
    Validator.validateNotNull(channelOutTradeNo,
        "未查询到对应订单信息，channelTradeNo:" + channelOutTradeNo);
    DivisionParamDTO payReqDTO = new DivisionParamDTO(channelOutTradeNo, order.getAmount());
    return CommonResult.ok(yseDivisionService.division(payReqDTO));
  }
}
